#------------------------------------------------------------------
#             IB Session
# wrapper around IB library
#------------------------------------------------------------------
import config as cfg
from ib.ext.Contract import Contract

import ibsession as ibs
import random
import pdb


#------------------------------------------------------------------
#             Globals
#------------------------------------------------------------------
session = ibs.IBsession()
#------------------------------------------------------------------
#             init()
#------------------------------------------------------------------

#------------------------------------------------------------------
#            contract constructor
#------------------------------------------------------------------
def buildContract(symbol, **kwargs):
     contract = Contract()
     contract.m_symbol = symbol
     contract.m_exchange = 'SMART'
     contract.m_currency = 'USD'
     contract.m_secType = 'STK'

     if "K" in kwargs:
         contract.m_secType = 'OPT'
         contract.m_strike = kwargs["K"]
     if "T" in kwargs:
         contract.m_expiry = kwargs["T"]
     if "right" in kwargs:
         contract.m_right = kwargs["right"]
         
     return contract

#------------------------------------------------------------------
#            quotefetchers
#------------------------------------------------------------------
def fetchsimple():
     return [str(m) for m in cfg.msgq.queue]

def fetchstraddle():
     print 'call: ' + cfg.msgq.get() + ' '
     print 'put: ' + cfg.msgq.get() + ' '
         

def quotefetcher(sectype = 'spot'):
     """ default is spot,call,put and will call fetchsymple"""
     return {
          'straddle': fetchstraddle,
          }.get(sectype, fetchsimple)

#------------------------------------------------------------------
#             getQuote()
#------------------------------------------------------------------
def getQuote(symbol,sectype = 'spot',**kwargs):
    global session
    contract = buildContract(symbol,**kwargs)
    print session.reqSnapshotMktData(contract)

#------------------------------------------------------------------
#            class quote
#------------------------------------------------------------------
class quote:
     def __init__(symbol,sectype,ask,**kwargs):
          if 'bid' in kwargs:
              self.bid = bid
          if 'last' in kwargs:
              self.last
          if 'ivol' in kwargs:
              self.ivol
         
#------------------------------------------------------------------
#             getquote()
#------------------------------------------------------------------
def getquote(symbol,sectype = 'spot',**kwargs):
     """ retrieve list of quote objs"""
     global session
     quotes = list()

     contract = buildContract(symbol,**kwargs)
     if (sectype == 'straddle'):
         call = buildContract(symbol, right='CALL', K=kwargs['K'],
                              T = cfg.front)
         put = buildContract(symbol, right='PUT', K=kwargs['K'],
                             T = cfg.front)
  
         quotes.append(session.reqSnapshotMktData(call))
         quotes.append(session.reqSnapshotMktData(put))
         
     if (sectype == 'spot'):
         spot = buildContract(symbol,**kwargs)
         quotes.append(session.reqSnapshotMktData(spot))

     if (sectype == 'call'):
         call = buildContract(symbol, right='CALL', K=kwargs['K'],
                              T = cfg.front)
         quotes.append(session.reqSnapshotMktData(call))

         
     return quotes
        
#------------------------------------------------------------------
#             level
#------------------------------------------------------------------
def level(symbol, strike= None, details=False, theo=False):
     """ return straddle in pts and ivol"""
#TODO: remove K argument by default find the nearest ATM K
     quotes = getquote(symbol, sectype='straddle', K=strike)
     
     
     
#------------------------------------------------------------------
#             skew
#------------------------------------------------------------------

#------------------------------------------------------------------
#             wabble = dPrice/dSkew
#------------------------------------------------------------------

#------------------------------------------------------------------
#             position(ticket) (display position in the skew map
#------------------------------------------------------------------

    
 #  connection.reqMktData(contract.tickerid, contract, '', True)    

